package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.mapper.InstitutionDeviceMapper;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.InstitutionDevice;
import com.wty.cmims.pojo.InstitutionDevice;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.InstitutionDeviceService;
import com.wty.cmims.mapper.InstitutionDeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【institution_device】的数据库操作Service实现
* @createDate 2024-12-22 13:30:31
*/
@Service
public class InstitutionDeviceServiceImpl extends ServiceImpl<InstitutionDeviceMapper, InstitutionDevice>
    implements InstitutionDeviceService{
    @Autowired
    private InstitutionDeviceMapper institutionDeviceMapper;
    @Override
    public ResponseResult<Map<String, Object>> findIDeviceByInstitutionId(Map<String,Object> paramsMap) {
        // 分页
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        Integer pageSize = (Integer) paramsMap.get("pageSize");
        paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
        responseResult.setPageSize(pageSize);

        List<Map<String, Object>> institutionDevices = institutionDeviceMapper.findIDeviceByInstitutionId(paramsMap);
        Integer total = institutionDeviceMapper.findIDeviceNum(paramsMap);
        if (institutionDevices != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询医疗机构——医疗器械信息成功");
            responseResult.setDatas(institutionDevices);
            responseResult.setTotal(total);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询医疗机构——医疗器械信息失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> updateIDeviceStock(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        LambdaUpdateWrapper<InstitutionDevice> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(InstitutionDevice::getInstitutionId, paramsMap.get("institution_id"))
                .eq(InstitutionDevice::getDeviceId, paramsMap.get("device_id"))
                .set(InstitutionDevice::getLastMaintenanceDate, paramsMap.get("last_maintenance_date"))
                .set(InstitutionDevice::getStatus, paramsMap.get("status"));
        boolean update = this.update(wrapper);
        if (update) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("维护日期修改成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("维护日期修改失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> addIDevice(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        InstitutionDevice institutionDevice = new InstitutionDevice();

        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            if (paramsMap.get("last_maintenance_date") != null){
                date = sdfDay.parse(paramsMap.get("last_maintenance_date").toString());
            }
        } catch (ParseException e) {
            System.out.println("日期转换错误");
        }
        if (date != null) {
            paramsMap.put("last_maintenance_date", date);
        }

        institutionDevice.setInstitutionId(Integer.parseInt((String) paramsMap.get("institution_id")));
        institutionDevice.setDeviceId((Integer) paramsMap.get("device_id"));
        institutionDevice.setLastMaintenanceDate((Date) paramsMap.get("last_maintenance_date"));
        institutionDevice.setStatus((Integer) paramsMap.get("status"));
        boolean save = this.save(institutionDevice);
        if (save) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("医疗器械添加成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("医疗器械添加失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> deleteIDevice(Integer institutionId, Integer deviceId) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        LambdaUpdateWrapper<InstitutionDevice> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(InstitutionDevice::getInstitutionId,institutionId);
        wrapper.eq(InstitutionDevice::getDeviceId,deviceId);
        wrapper.set(InstitutionDevice::getIsDel,1);
        boolean delete = this.update(wrapper);
        if (delete) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("医疗器械删除成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("医疗器械删除失败");
        }
        return responseResult;
    }
}




